package com.ruoyi.mobile.project.oms.service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.db.oms.domain.OmsCart;
import com.ruoyi.db.oms.domain.query.OmsCartQueryVo;
import com.ruoyi.db.oms.mapper.OmsCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class WxOmsCartService
{
    @Autowired
    private OmsCartMapper cartMapper;

    /**
     * 判断购物车中是否存在此规格商品
     *
     * @author 严方亚
     * @date 2020-05-22
     */
    public OmsCart queryExist(Long goodsId, Long productId, Long userId)
    {
        OmsCartQueryVo cartQueryVo = new OmsCartQueryVo();
        cartQueryVo.setUserId(userId);
        cartQueryVo.setGoodsId(goodsId);
        cartQueryVo.setProductId(productId);
        return cartMapper.selectOmsCart(cartQueryVo);
    }

    /**
     * 添加购物车
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param cart
     */
    public void add(OmsCart cart)
    {
        cart.setAddTime(LocalDateTime.now());
        cart.setUpdateTime(LocalDateTime.now());
        cartMapper.insertOmsCart(cart);
    }

    /**
     * 更新购物车
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param cart
     */
    public int updateById(OmsCart cart)
    {
        cart.setUpdateTime(LocalDateTime.now());
        return cartMapper.updateOmsCart(cart);
    }

    /**
     * 获取购物车列表
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     */
    public List<OmsCart> queryByUid(Long userId)
    {
        OmsCartQueryVo cartQueryVo = new OmsCartQueryVo();
        cartQueryVo.setUserId(userId);
        return cartMapper.selectOmsCartList(cartQueryVo);
    }

    /**
     * 获取购物车选中商品列表
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     */
    public List<OmsCart> queryByUidAndChecked(Long userId)
    {
        OmsCartQueryVo cartQueryVo = new OmsCartQueryVo();
        cartQueryVo.setUserId(userId);
        cartQueryVo.setChecked(true);
        return cartMapper.selectOmsCartList(cartQueryVo);
    }

    /**
     * 删除购物车
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     */
    public int delete(List<Long> productIdList, Long userId)
    {
        Long[] productIds = productIdList.stream().toArray(Long[]::new);
        return cartMapper.deleteCartByUId(productIds, userId);
    }

    /**
     * 订单查询
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param id
     */
    public OmsCart findById(Long id)
    {
        return cartMapper.selectOmsCartById(id);
    }

    /**
     * 订单查询
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     * @param id
     */
    public OmsCart findById(Long userId, Long id)
    {
        OmsCartQueryVo cartQueryVo = new OmsCartQueryVo();
        cartQueryVo.setUserId(userId);
        cartQueryVo.setId(id);
        return cartMapper.selectOmsCart(cartQueryVo);
    }

    /**
     * 更新购物车商品选中状态
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     * @param idsList
     * @param checked
     */
    public int updateCheck(Long userId, List<Long> idsList, Boolean checked)
    {
        OmsCart cart = new OmsCart();
        cart.setChecked(checked);
        cart.setUpdateTime(LocalDateTime.now());
        Long[] productIds = idsList.stream().toArray(Long[]::new);
        return cartMapper.updateOmsCartChecked(cart, userId, productIds);
    }

    /**
     * 清空购物车
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     */
    public void clearGoods(Long userId)
    {
        cartMapper.deleteOmsCartByUserId(userId);
    }

    public List<OmsCart> querySelective(Long userId, Long goodsId, Integer page, Integer limit, String sort, String order)
    {
        return null;
    }

    /**
     * 删除购物车商品
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param id
     */
    public void deleteById(Long id)
    {
        cartMapper.deleteOmsCartById(id);
    }

    public boolean checkExist(Long goodsId)
    {
        return true;
    }

    public void updateProduct(Long id, String goodsSn, String goodsName, BigDecimal price, String url)
    {

    }
}
